//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  Helpers.cs
//    
// Abstract:
//
//  This module implements some commonly used helper methods for Microsoft Office Communications
//  Server applications.
//  
//--

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Microsoft.OfficeCommunicationsServer.Applications.Common
{
    /// <summary>
    /// Helper methods used in Office Communications Server applications.
    /// </summary>
    public static class Helpers
    {
        /// <summary>
        /// Get the value for the specified app setting from the config file.
        /// </summary>
        /// <param name="settingName">Name of the app setting</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>
        /// The value from the config, or the default value if the setting can't be read
        /// </returns>
        public static T GetSetting<T>(string settingName, T defaultValue)
        {
            object settingValue = null;
            Logger.WriteTraceInformation(string.Format("Attempting to retrieve setting '{0}' from config.",settingName));
            try
            {
                settingValue = System.Configuration.ConfigurationManager.AppSettings[settingName];
            }
            catch (ConfigurationErrorsException)
            {
                // The setting must not be present so ignore this exception and return the default value.
            }

            if (settingValue == null)
            {
                return defaultValue;
            }

            return (T)System.Convert.ChangeType(settingValue, typeof(T), null);
        }

        /// <summary>
        /// Debug.Assert that value is of type T, then return (T)value 
        /// </summary>
        /// <typeparam name="T">Type of value</typeparam>
        /// <param name="value">Object to be cast to T</param>
        /// <returns>(T)value</returns>
        public static T AssertCast<T>(this object value)
        {
            Debug.Assert(null == value || value is T, "null == value || value is T");

            return (T)value;
        }

        /// <summary>
        /// Clean up temporary file without throwing exceptions
        /// </summary>
        /// <param name="fileName"></param>
        public static void CleanUpTempFile(string fileName)
        {
            Debug.Assert(!String.IsNullOrEmpty(fileName), "fileName is empty");
            try
            {
                File.Delete(fileName);
            }
            catch (DirectoryNotFoundException exn)
            {
                Logger.WriteTraceError(exn, "Deleting " + fileName + " failed.");
            }
            catch (PathTooLongException exn)
            {
                Logger.WriteTraceError(exn, "Deleting " + fileName + " failed.");
            }
            catch (IOException exn)
            {
                Logger.WriteTraceError(exn, "Deleting " + fileName + " failed.");
            }
            catch (NotSupportedException exn)
            {
                Logger.WriteTraceError(exn, "Deleting " + fileName + " failed.");
            }
            catch (UnauthorizedAccessException exn)
            {
                Logger.WriteTraceError(exn, "Deleting " + fileName + " failed.");
            }
        }


        /// <summary>
        /// Call this method from any exception handler that catches System.Exception
        /// This method checks the type of exception and, if it is an irrecoverable exception,
        /// logs it before re-throwing it.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "ex")]
        public static void FilterIrrecoverableExceptions(Exception ex)
        {
        }
    }
}
